#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    socket=new QTcpSocket(this);

    //建立信号与槽，使得连接成功后弹出小窗口提示
    connect(socket,&QTcpSocket::connected,[this]()
    {
        QMessageBox::information(this,"连接提示","连接成功");
        qDebug()<<"连接成功";
    });

    //发起连接
    socket->connectToHost(QHostAddress(IP), PORT);
    //最多花10秒时间连接服务器
    if (!socket->waitForConnected(10000)) {
        qDebug() << "Connection timed out.";
    }

    //应用程序输出信息提示是否连接成功
    if(!socket->waitForConnected()){
        qDebug() << "连接失败: " << socket->errorString();
    }

    //收到回复
    connect(socket,&QTcpSocket::readyRead,this,&Widget::server_reply_slot);

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    QString app_id = ui->appLineEdit->text();
    QJsonObject obj;
    obj.insert("cmd","search_bind");
    obj.insert("appid",app_id);

    //向服务器发送数据，查询是否已经绑定
    QByteArray ba = QJsonDocument(obj).toJson();
    socket->write(ba);

}

void Widget::server_reply_slot()
{
    QByteArray ba = socket->readAll();
    QJsonObject obj = QJsonDocument::fromJson(ba).object();
    QString cmd = obj.value("cmd").toString();
    if(cmd == "reply_bind")
    {
        QString result = obj.value("result").toString();
        if("yes" == result)
        {
            qDebug()<<"已经绑定";

            //断开信号与槽的连接
            disconnect(socket,0,this,0);
            //跳转到新的界面
            Player *p = new Player(socket);
            p->show();
            this->close();
        }
        else if("no" == result)
        {
            qDebug()<<"未绑定";

            //开始绑定
            bind_operation();
        }
    }
    else if(cmd == "bind_success")
    {
        qDebug()<<"绑定成功";

        //断开信号与槽的连接
        disconnect(socket,0,this,0);
        //跳转到新的界面
        Player *p = new Player(socket);
        p->show();
        this->close();
    }

}

void Widget::bind_operation()
{
    QString app_id = ui->appLineEdit->text();
    QString dev_id = ui->devLineEdit->text();

    QJsonObject obj;
    obj.insert("cmd","bind");
    obj.insert("appid",app_id);
    obj.insert("deviceid",dev_id);

    //QJsonObject转换成QByteArray
    QByteArray ba = QJsonDocument(obj).toJson();
    socket->write(ba);

}
